from Parameter import *

def simpleParse(mainString, beginString, endString ):
    """Searches for a substring between beginString and endString"""
    posBeginString = mainString.find(beginString) + len(beginString)
    posEndString = mainString.find(endString)
    resultado = mainString[posBeginString:posEndString]
    return resultado


def extraer_int_tag(datos_arch, tag):
    """ Extracts data from string datos_str, delimited by <tag> y </tag>
        and convets it to integer numbers (list of integers)"""
    beginString = '<' + tag + '>'
    endString = '</' + tag + '>'
    str_parse = simpleParse(datos_arch, beginString, endString)
    str_canal = str_parse.split(',')

    canal = []
    n = len(str_canal)
    for i in range(n):
        # print(str_canal[i])
        canal.append(int(str_canal[i]))
    return canal


def read_file():
    file_object = open(file_name, 'r')
    # 读取整个文件，放到一个字符串变量中
    all_text = file_object.read()

    # 以L1,L2,L3标志为界限，分别得到每个通道的值
    canal_1 = extraer_int_tag(all_text, 'L1')
    canal_2 = extraer_int_tag(all_text, 'L2')
    canal_3 = extraer_int_tag(all_text, 'L3')
    print("Amount of samples in channel 1: %s" % len(canal_1))
    print("Amount of samples in channel 2: %s" % len(canal_2))
    print("Amount of samples in channel 3: %s" % len(canal_3))

    return [canal_1, canal_2, canal_3]


